package com.example.myapplication8;
// MyAdapter.java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<DataModel> dataList;
    private final OnItemClickCallback onItemClick;
    private final OnItemLongClickCallback onItemLongClick;

    public MyAdapter(OnItemClickCallback onItemClick, OnItemLongClickCallback onItemLongClick) {
        this.onItemClick = onItemClick;
        this.onItemLongClick = onItemLongClick;
    }

    public void updateData(List<DataModel> newData) {
        this.dataList = newData;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DataModel item = dataList.get(position);
        holder.tvTitle.setText(item.getTitle());

        holder.itemView.setOnClickListener(v -> onItemClick.onClick(item));
        holder.itemView.setOnLongClickListener(v -> {
            onItemLongClick.onLongClick(item);
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;

        ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(android.R.id.text1);
        }
    }

    public interface OnItemClickCallback {
        void onClick(DataModel item);
    }

    public interface OnItemLongClickCallback {
        void onLongClick(DataModel item);
    }
}